define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'byd_pricture/index' + location.search,
                    add_url: 'byd_pricture/add',
                    edit_url: 'byd_pricture/edit',
                    del_url: 'byd_pricture/del',
                    multi_url: 'byd_pricture/multi',
                    import_url: 'byd_pricture/import',
                    table: 'byd_pricture',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'bydshop.name', title: __('产品名称'), operate: 'LIKE'},

                        {field: 'type', title: __('类型'), searchList: {"1":__('轮播图'),"2":__('详情图'),"3":__('封面图'),"4":__('VR图片'),"5":__('视频'),"6":__('设计师图片'),"7":__('设计师灵感')}, formatter: Table.api.formatter.normal},
                        // {field: 'shopid', title: __('Shopid')},
                        // {field: 'txid', title: __('Txid')},
                        {field: 'link', title: __('位置'), searchList: {"0":__('产品'),"1":__('套系')}, formatter: Table.api.formatter.normal},
                        // {field: 'head', title: __('Head'), operate: 'LIKE'},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'status', title: __('Status'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        {field: 'image', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},




                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'byd_pricture/select',
                }
            });
            var urlArr = [];
            var multiple = Backend.api.query('multiple');
            multiple = multiple === 'true' ? true : false;

            var table = $("#table");

            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
                if (e.type == 'check' || e.type == 'uncheck') {
                    row = [row];
                } else {
                    urlArr = [];
                }
                $.each(row, function (i, j) {
                    if (e.type.indexOf("uncheck") > -1) {
                        var index = urlArr.indexOf(j.url);
                        if (index > -1) {
                            urlArr.splice(index, 1);
                        }
                    } else {
                        urlArr.indexOf(j.url) == -1 && urlArr.push(j.url);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'bydshop.name', title: __('产品名称'), operate: 'LIKE'},
                        {field: 'type', title: __('类型'), searchList: {"1":__('轮播图'),"2":__('详情图'),"3":__('封面图'),"4":__('VR图片'),"5":__('视频'),"6":__('设计师图片')}, formatter: Table.api.formatter.normal},
                        // {field: 'shopid', title: __('Shopid')},
                        // {field: 'txid', title: __('Txid')},
                        {field: 'link', title: __('位置'), searchList: {"0":__('产品'),"1":__('套系')}, formatter: Table.api.formatter.normal},
                        // {field: 'head', title: __('Head'), operate: 'LIKE'},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'status', title: __('Status'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        {field: 'image', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    url=row.image;
                                    //+'-'+row.image
                                    Fast.api.close({url:url,multiple: false});
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = [];
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    urlArr.push(j.id+'-'+j.goodsname);
                });
                console.log(urlArr);
                var multiple = Backend.api.query('multiple');
                // multiple=true;

                // multiple = multiple == 'true' ? true : false;

                Fast.api.close({url: urlArr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            require(['upload'], function (Upload) {
                Upload.api.plupload($("#toolbar .plupload"), function () {
                    $(".btn-refresh").trigger("click");
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});